package com.huanletao.compress;

import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipParameters;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.util.zip.Deflater;

/**
 * <p>
 *   sddf
 * </p>
 *
 * @package: com.huanletao.compress
 * @description:
 * @className: Demo
 * @author: @nncskj admin
 * @date: Created in 2022/5/30
 * @copyright: Copyright (c) 2022/5/30
 * @version: V1.0
 */
public class Demo {
    public static void main(String[] args) throws IOException {

        // gzip压缩
        String file = "/test.js";
        GzipParameters parameters = new GzipParameters();
        parameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
        parameters.setOperatingSystem(3);
        parameters.setFilename(FilenameUtils.getName(file));
        parameters.setComment("Test file");
        parameters.setModificationTime(System.currentTimeMillis());
        FileOutputStream fos = new FileOutputStream(file + ".gz");
        try (GzipCompressorOutputStream gzos = new GzipCompressorOutputStream(fos, parameters);
             InputStream is = new FileInputStream(file)) {
            IOUtils.copy(is, gzos);
        }
        // gzip解压
        String gzFile = "/test.js.gz";
        FileInputStream is = new FileInputStream(gzFile);
        try (GzipCompressorInputStream gis = new GzipCompressorInputStream(is)) {
            GzipParameters p = gis.getMetaData();
            File targetFile = new File("/test.js");
          //  FileUtils.copyFile(gis, targetFile);
            targetFile.setLastModified(p.getModificationTime());
        }
    }
}
